package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;

import model.FreeStudent;
import model.OfferedCourse;
import model.Registration;
import model.RetrainingStudent;
import model.Student;
import persistency.controller.FreeStudentController;
import persistency.controller.OfferedCourseController;
import persistency.controller.OfferedCourseRegistrationController;
import persistency.controller.RegistrationController;
import persistency.controller.RetrainingStudentController;
import persistency.controller.StudentController;
import util.Date;
import controller.StartupController;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class RegistrationFrame extends JFrame {

	private StartupController startupController;
	private JComboBox jComboBoxStudent;
	private JLabel jLabelStudent;
	private JLabel jLabelStudentType;
	private JCheckBox jCheckBoxReceiveEmail;
	private JLabel jLabelMessage;
	private JLabel jLabelRetraingReasonValue;
	private JLabel jLabelRetrainingReason;
	private JLabel jLabelHighestDegree;
	private JLabel jLabelHighestDegreevalue;
	private JLabel jLabelStudentTypeValue;
	private JLabel jLabelNationalityValue;
	private JLabel jLabelNationality;
	private JLabel jLabelStudentNameValue;
	private JLabel jLabelStudentNumberValue;
	private JLabel jLabelStudentName;
	private JLabel jLabelStudentNumber;
	private JDesktopPane jDesktopPaneStudentInfo;
	private JLabel jLabelDateValue;
	private JComboBox jComboBoxOfferedCourse;
	private JLabel jLabelCourse;
	private JLabel jLabelTeacherValue;
	private JLabel jLabelPlacesValue;
	private JLabel jLabelCourseValue;
	private JLabel jLabelDate;
	private JLabel jLabelNumPlaces;
	private JLabel jLabelTeacher;
	private JDesktopPane jDesktopPane1;
	private JButton jButtonConfirm;

	public RegistrationFrame(StartupController startupController) {
		super("Register for courses for schoolyear 2011-2012");
		this.startupController = startupController;

		java.awt.Container container = getContentPane();
		container.setBackground(Color.pink);
		container.setLayout(null);
		container.setEnabled(false);
		container.setPreferredSize(new java.awt.Dimension(494, 493));

		JLabel lblStartDate = new JLabel("Start date :");

		container.add(lblStartDate);
		{
			jButtonConfirm = new JButton();
			container.add(jButtonConfirm);
			jButtonConfirm.setText("Confirm");
			jButtonConfirm.setBounds(368, 425, 102, 21);
			jButtonConfirm.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					Registration registration = new Registration();
					registration.setOfferedCourse((OfferedCourse) jComboBoxOfferedCourse.getSelectedItem());
					registration.setStudent((Student) jComboBoxStudent.getSelectedItem());
					registration.setRegistrationDate(new Date());
					registration.setReceiveCourseEmail(jCheckBoxReceiveEmail.isSelected());
					try {
						registration.performValidate();
						persistency.controller.RegistrationController.instance().insertRegistration(registration);
						registration.setId(persistency.controller.RegistrationController.instance().getlastId());
						OfferedCourseRegistrationController.instance().insertOfferedCourseRegistration(registration);
						jLabelMessage.setForeground(Color.GREEN);
						jLabelMessage.setText("Registration succesfull");
					} catch (Exception e) {
						jLabelMessage.setText(e.getMessage());
						jLabelMessage.setForeground(Color.RED);
					}
				}
			});
		}
		{
			jDesktopPane1 = new JDesktopPane();
			container.add(jDesktopPane1);
			jDesktopPane1.setBounds(12, 20, 458, 169);
			{
				ComboBoxModel jComboBoxOfferedCourseModel = new DefaultComboBoxModel(OfferedCourseController.instance().getAll().toArray());
				jComboBoxOfferedCourse = new JComboBox();
				jDesktopPane1.add(jComboBoxOfferedCourse, JLayeredPane.DEFAULT_LAYER);
				jComboBoxOfferedCourse.setModel(jComboBoxOfferedCourseModel);
				jComboBoxOfferedCourse.setBounds(100, 17, 301, 21);
				jComboBoxOfferedCourse.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent evt) {
						setOfferedcourseData((OfferedCourse) jComboBoxOfferedCourse.getSelectedItem());
					}
				});
			}
			{
				JLabel lblCourses = new JLabel("Select offered course:");
				jDesktopPane1.add(lblCourses, JLayeredPane.DEFAULT_LAYER);
				lblCourses.setBounds(12, 12, 119, 30);
				lblCourses.setText("Select course:");
			}
			{
				jLabelCourse = new JLabel();
				jDesktopPane1.add(jLabelCourse, JLayeredPane.DEFAULT_LAYER);
				jLabelCourse.setText("Course :");
				jLabelCourse.setBounds(40, 48, 72, 14);
			}
			{
				jLabelTeacher = new JLabel();
				jDesktopPane1.add(jLabelTeacher, JLayeredPane.DEFAULT_LAYER);
				jLabelTeacher.setText("Teacher :");
				jLabelTeacher.setBounds(40, 74, 72, 14);
			}
			{
				jLabelNumPlaces = new JLabel();
				jDesktopPane1.add(jLabelNumPlaces, JLayeredPane.DEFAULT_LAYER);
				jLabelNumPlaces.setText("Places :");
				jLabelNumPlaces.setBounds(40, 100, 72, 14);
			}
			{
				jLabelDate = new JLabel();
				jDesktopPane1.add(jLabelDate, JLayeredPane.DEFAULT_LAYER);
				jLabelDate.setText("Date :");
				jLabelDate.setBounds(40, 126, 72, 14);
			}
			{
				jLabelCourseValue = new JLabel();
				jDesktopPane1.add(jLabelCourseValue, JLayeredPane.DEFAULT_LAYER);
				jLabelCourseValue.setBounds(145, 50, 301, 14);
				jLabelCourseValue.setText("Default");
			}
			{
				jLabelTeacherValue = new JLabel();
				jDesktopPane1.add(jLabelTeacherValue, JLayeredPane.DEFAULT_LAYER);
				jLabelTeacherValue.setText("Default");
				jLabelTeacherValue.setBounds(144, 76, 302, 14);
			}
			{
				jLabelPlacesValue = new JLabel();
				jDesktopPane1.add(jLabelPlacesValue, JLayeredPane.DEFAULT_LAYER);
				jLabelPlacesValue.setText("Default");
				jLabelPlacesValue.setBounds(144, 102, 302, 14);
			}
			{
				jLabelDateValue = new JLabel();
				jDesktopPane1.add(jLabelDateValue, JLayeredPane.DEFAULT_LAYER);
				jLabelDateValue.setText("Default");
				jLabelDateValue.setBounds(144, 128, 302, 14);
			}
		}
		{
			jDesktopPaneStudentInfo = new JDesktopPane();
			container.add(jDesktopPaneStudentInfo);
			jDesktopPaneStudentInfo.setBounds(12, 214, 458, 199);
			{

				List<Student> students = new ArrayList<Student>();
				students.addAll(FreeStudentController.instance().getAll());
				students.addAll(RetrainingStudentController.instance().getAll());
				jComboBoxStudent = new JComboBox(students.toArray());
				jDesktopPaneStudentInfo.add(jComboBoxStudent, JLayeredPane.DEFAULT_LAYER);

				jComboBoxStudent.setBounds(102, 12, 301, 21);
				jComboBoxStudent.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent evt) {
						setStudentData((Student) jComboBoxStudent.getSelectedItem());
					}
				});
			}
			{
				jLabelStudent = new JLabel();
				jDesktopPaneStudentInfo.add(jLabelStudent, JLayeredPane.DEFAULT_LAYER);
				jLabelStudent.setText("Select student :");
				jLabelStudent.setBounds(7, 15, 119, 14);
			}
			{
				jLabelStudentNumber = new JLabel();
				jDesktopPaneStudentInfo.add(jLabelStudentNumber, JLayeredPane.DEFAULT_LAYER);
				jLabelStudentNumber.setText("Number :");
				jLabelStudentNumber.setBounds(37, 41, 107, 14);
			}
			{
				jLabelStudentName = new JLabel();
				jDesktopPaneStudentInfo.add(jLabelStudentName, JLayeredPane.DEFAULT_LAYER);
				jLabelStudentName.setText("Name :");
				jLabelStudentName.setBounds(37, 67, 107, 14);
			}
			{
				jLabelStudentNumberValue = new JLabel();
				BoxLayout jLabelStudentNumberValueLayout = new BoxLayout(jLabelStudentNumberValue, javax.swing.BoxLayout.Y_AXIS);
				jLabelStudentNumberValue.setLayout(jLabelStudentNumberValueLayout);
				jDesktopPaneStudentInfo.add(jLabelStudentNumberValue, JLayeredPane.DEFAULT_LAYER);
				jLabelStudentNumberValue.setText("Default");
				jLabelStudentNumberValue.setBounds(150, 41, 301, 14);
			}
			{
				jLabelStudentNameValue = new JLabel();
				BoxLayout jLabel1Layout = new BoxLayout(jLabelStudentNameValue, javax.swing.BoxLayout.Y_AXIS);
				jLabelStudentNameValue.setLayout(jLabel1Layout);
				jDesktopPaneStudentInfo.add(jLabelStudentNameValue, JLayeredPane.DEFAULT_LAYER);
				jLabelStudentNameValue.setText("Default");
				jLabelStudentNameValue.setBounds(150, 67, 301, 14);
			}
			{
				jLabelNationality = new JLabel();
				jDesktopPaneStudentInfo.add(jLabelNationality, JLayeredPane.DEFAULT_LAYER);
				jLabelNationality.setText("Nationality :");
				jLabelNationality.setBounds(36, 119, 108, 14);
			}
			{
				jLabelNationalityValue = new JLabel();
				BoxLayout jLabelNationalityValueLayout = new BoxLayout(jLabelNationalityValue, javax.swing.BoxLayout.Y_AXIS);
				jLabelNationalityValue.setLayout(jLabelNationalityValueLayout);
				jDesktopPaneStudentInfo.add(jLabelNationalityValue, JLayeredPane.DEFAULT_LAYER);
				jLabelNationalityValue.setText("Default");
				jLabelNationalityValue.setBounds(150, 119, 274, 14);
			}
			{
				jLabelStudentType = new JLabel();
				jDesktopPaneStudentInfo.add(jLabelStudentType, JLayeredPane.DEFAULT_LAYER);
				jLabelStudentType.setText("Type :");
				jLabelStudentType.setBounds(37, 93, 107, 14);
			}
			{
				jLabelStudentTypeValue = new JLabel();
				BoxLayout jLabelStudentTypeValueLayout = new BoxLayout(jLabelStudentTypeValue, javax.swing.BoxLayout.Y_AXIS);
				jLabelStudentTypeValue.setLayout(jLabelStudentTypeValueLayout);
				jDesktopPaneStudentInfo.add(jLabelStudentTypeValue, JLayeredPane.DEFAULT_LAYER);
				jLabelStudentTypeValue.setText("Default");
				jLabelStudentTypeValue.setBounds(150, 93, 299, 14);
			}
			{
				jLabelHighestDegree = new JLabel();
				jDesktopPaneStudentInfo.add(jLabelHighestDegree, JLayeredPane.DEFAULT_LAYER);
				jLabelHighestDegree.setText("Highest degree :");
				jLabelHighestDegree.setBounds(36, 145, 111, 14);
			}
			{
				jLabelHighestDegreevalue = new JLabel();
				BoxLayout jLabelHighestDegreevalueLayout = new BoxLayout(jLabelHighestDegreevalue, javax.swing.BoxLayout.Y_AXIS);
				jLabelHighestDegreevalue.setLayout(jLabelHighestDegreevalueLayout);
				jDesktopPaneStudentInfo.add(jLabelHighestDegreevalue, JLayeredPane.DEFAULT_LAYER);
				jLabelHighestDegreevalue.setText("Default");
				jLabelHighestDegreevalue.setBounds(153, 145, 271, 14);
			}
			{
				jLabelRetrainingReason = new JLabel();
				jDesktopPaneStudentInfo.add(jLabelRetrainingReason, JLayeredPane.DEFAULT_LAYER);
				jLabelRetrainingReason.setText("Retraining reason :");
				jLabelRetrainingReason.setBounds(33, 171, 114, 14);
			}
			{
				jLabelRetraingReasonValue = new JLabel();
				BoxLayout jLabelRetraingReasonValueLayout = new BoxLayout(jLabelRetraingReasonValue, javax.swing.BoxLayout.Y_AXIS);
				jLabelRetraingReasonValue.setLayout(jLabelRetraingReasonValueLayout);
				jDesktopPaneStudentInfo.add(jLabelRetraingReasonValue, JLayeredPane.DEFAULT_LAYER);
				jLabelRetraingReasonValue.setText("Default");
				jLabelRetraingReasonValue.setBounds(153, 171, 271, 14);
			}
		}
		{
			jLabelMessage = new JLabel();
			container.add(jLabelMessage);
			jLabelMessage.setBounds(12, 443, 338, 21);
		}
		{
			jCheckBoxReceiveEmail = new JCheckBox();
			container.add(jCheckBoxReceiveEmail);
			jCheckBoxReceiveEmail.setText("Recieve course email?");
			jCheckBoxReceiveEmail.setBounds(12, 422, 123, 18);
		}

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				showMenu();
			}
		});

		this.setSize(500, 500);
		setStudentData((Student) jComboBoxStudent.getSelectedItem());
		setOfferedcourseData((OfferedCourse) jComboBoxOfferedCourse.getSelectedItem());

	}

	private void setStudentData(Student student) {
		if (student != null) {
			jLabelStudentNumberValue.setText(student.getStudentNumber());
			jLabelStudentNameValue.setText(student.toString());
			if (student instanceof FreeStudent) {
				jLabelStudentTypeValue.setText("Free student");
				jLabelNationalityValue.setText(((FreeStudent) student).getNationality());
				jLabelNationalityValue.setVisible(true);
				jLabelRetraingReasonValue.setVisible(false);
				jLabelHighestDegreevalue.setVisible(false);

			} else if (student instanceof RetrainingStudent) {
				RetrainingStudent retrainingstudent = (RetrainingStudent) student;
				jLabelStudentTypeValue.setText("Retraining student");
				jLabelRetraingReasonValue.setText(retrainingstudent.getRetrainingReason());
				jLabelHighestDegreevalue.setText(retrainingstudent.getHighestDegree());
				jLabelNationalityValue.setVisible(false);
				jLabelRetraingReasonValue.setVisible(true);
				jLabelHighestDegreevalue.setVisible(true);
			}
		}
	}

	private void setOfferedcourseData(OfferedCourse offeredCourse) {
		jLabelCourseValue.setText(offeredCourse.getCourse().toString());
		jLabelDateValue.setText(offeredCourse.getTimeBlock().toString());
		jLabelPlacesValue.setText(Integer.toString(offeredCourse.getNumberOfPlaces()));
		jLabelTeacherValue.setText(offeredCourse.getTeacher().toString());
	}

	private void showMenu() {
		startupController.execute();
	}

}
